
 
import java.util.concurrent.TimeUnit;
 
public class DieLock {
    public static void main(String[] args) {
 
 
         Object A = new Object();
 
         Object B = new Object();
 
         //线程1

        Thread thread1 = new Thread(()->{
            synchronized (A){
                System.out.println("线程1此时获取锁A");
                try {

                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //对象A苏醒之后再去尝试获取锁2
                synchronized (B){
                    System.out.println("线程1此时获取锁B");
                }
            }
        });
 
        thread1.start();
        //线程2
        Thread thread2 = new Thread(()->{
            synchronized (B){
                System.out.println("线程2此时获取锁B");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //对象A苏醒之后再去尝试获取锁2
                synchronized (A){
                    System.out.println("线程2此时获取锁A");
                }
            }
        });
 
        thread2.start();
    }
 
}